﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AgentRelation.aspx.cs" Inherits="ZLCMS.Web.SystemManage.Basics.Shop.Agent.AgentRelation" %>

    <%@ Register Src="~/SystemManage/Control/SinglePictureControl.ascx" TagName="SinglePictureControl"
    TagPrefix="spc" %>

<div class="pageContent">   
   	<div class="panelBar">
		<ul class="toolBar">           
            <li><a class="icon" href="javascript:void(0)" onclick="navTab.reload();" title="刷新"><span>刷新</span></a></li>
 		</ul>
	</div>

    <div class="unitBox" style="float:left; display:block; overflow:auto; width:358px;">
<div class="pageHeader" style="border:1px #B8D0D6 solid">
	<div class="searchBar">
		<table class="searchContent" width="100%">
			<tr>
				<td width="300px"><h3>代理结构</h3>
				</td>
			</tr>
		</table>
	</div>
</div>
		
        
<div class="pageContent" style="border:1px #B8D0D6 solid;"  layoutH="70">

<div class="pageFormContent" layoutH="128">
<ul class="ztree" id="agentrelation_tree" setting='
{
	view: {
		addHoverDom: addHoverDom,
		removeHoverDom: removeHoverDom,
		selectedMulti: false
	},
    data: {
		key: {
			name: "long_name"
		},
		simpleData: {
			enable: true,
			idKey: "Account",
			pIdKey: "Parent"
		},
		keep: {
			parent: false,
			leaf: false
		}
	},
	async: {
		enable: true,
		type: "post",
		url: "/SystemManage/Basics/Shop/Agent/AgentInterface.aspx?opt=load_data",
        otherParam: ["temp", new Date().valueOf()]
	},
    edit: {
		enable: true,
		editNameSelectAll: false,
		showRemoveBtn: true,
		showRenameBtn: false,
		removeTitle: "删除",
		renameTitle: "修改"
	},
	callback: {
		beforeDrag: function (treeId, treeNodes) {
			return false;
		},
        onAsyncSuccess:function(event, treeId, treeNode, msg){
            var ztree = $.fn.zTree.getZTreeObj(treeId);
            var roots = ztree.getNodes();
            if(roots.length>0){
                ztree.expandNode(roots[0],true);
            }
        },

        beforeRemove:function(treeId, treeNode){
            alertMsg.confirm("请确认删除当前菜单项", {
                okCall: function() {
                    $.get("/SystemManage/Basics/Shop/Agent/AgentInterface.aspx", {opt:"remove", root:treeNode.Account, temp:new Date().valueOf()}, function(data){
                        $.fn.zTree.getZTreeObj(treeId).removeChildNodes(treeNode);
                        $.fn.zTree.getZTreeObj(treeId).removeNode(treeNode);
                        alertMsg.correct("操作成功");
                    }); 
                }
            });
            return false;
        },
        onClick:function(event, treeId, treeNode, clickFlag){
        $("#defaultpage",navTab.getCurrentPanel()).hide();
        $("#agentpage",navTab.getCurrentPanel()).show();
            $("#account_id",navTab.getCurrentPanel()).val(treeNode.Account);
            $("#account_name",navTab.getCurrentPanel()).val(treeNode.Account);
            $("#status",navTab.getCurrentPanel()).val(treeNode.Status);
            currentNode = treeNode;
            
            try{
            $("#agentBarcode").show();
            $(".image").first().attr("src","");
            
            var imgUrlObj=$.parseJSON(treeNode.BarcodeImg);
            if(imgUrlObj.length > 0){
                $(".image",navTab.getCurrentPanel()).first().attr("src",imgUrlObj[0].FilePath);
                $("#fc_open_value_json",navTab.getCurrentPanel()).val(treeNode.BarcodeImg);
            }else{
                $(".image",navTab.getCurrentPanel()).first().attr("src","https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + treeNode.Ticket);
            }
            var $img=$(".image",navTab.getCurrentPanel()).first();
            setTimeout(function () {
                if ($img.parent().height() > $img.height()) {
                    $img.css("margin-top", (($img.parent().height() - $img.height()) / 2) + "px");
                } else {
                    $img.css("margin-top", "0px");
                    $img.parent().height($img.height());
                }
            }, 200);
            }catch(e){}
            

            flushUserList(treeNode.Account);
        }
	}
}
'/>
</div>
<div class="formBar">
	    <ul>
			<li>
            <%if (ZLCMS.Commands.User.Util.AuthUtil.CheckAuthorization("AGENT_MAN", "ALL"))
              {%>
	       <input id="yewuyuanAcc" name="yewuyuan.Account" type="hidden"/>
<a class="button" href="/SystemManage/Basics/Shop/Agent/SelectAgentDialog.aspx" lookupGroup="yewuyuan"><span>添加代理商</span></a>
 
           <input id="qitaAcc" name="qita.Account" type="hidden"/>
<a id="selectMember" class="button" href="/SystemManage/Basics/Shop/Agent/SelectAgentDialog.aspx" lookupGroup="qita" style="display:none"><span>添加业务员</span></a>
<%} %>
</li>
		</ul>
</div>
<script type="text/javascript">
    var agentArray = <%=Newtonsoft.Json.JsonConvert.SerializeObject(RelationCode) %>;
    var accountTarget = agentArray[0];
    var relationCode = agentArray[0];
    var currentNode = {},parentNode = {}; 
    $('#yewuyuanAcc', navTab.getCurrentPanel()).bind('bringBack', function () {
        //alert('yewuyuan');
        addNode('agentrelation_tree', { AgentAccount: $(this).val(), ParentTarget: agentArray[0], RelationCode: agentArray[0] });
    });

    $('#qitaAcc', navTab.getCurrentPanel()).bind('bringBack', function () {
        //alert('qitaAcc');
     
        addNode('agentrelation_tree', { AgentAccount: $(this).val(), ParentAccount: accountTarget, RelationCode: relationCode }, parentNode);
    });
    function addNode(treeId, node, parentNode) {
        $.get("/SystemManage/Basics/Shop/Agent/AgentInterface.aspx", { opt: "add", data: $.toJSON(node), temp: new Date().valueOf() }, function (data) {
            var rsObj = $.parseJSON(data);
            if (rsObj.error) {
                alert(rsObj.error);
            } else {
                var zTree = $.fn.zTree.getZTreeObj(treeId);
                if (node.AccountTarget != agentArray[0]) {
                    zTree.addNodes(parentNode, rsObj);
                } else {
                    zTree.addNodes(null, rsObj);
                }
            }
        });
    } 
    
    function addHoverDom(treeId, treeNode) {
        var sObj = $("#" + treeNode.tId + "_span");
        if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0) return;
        var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
				+ "' title='增加下级代理' onfocus='this.blur();'></span>";
        sObj.after(addStr);
        var btn = $("#addBtn_" + treeNode.tId);
        if (btn) btn.bind("click", function () {
        
            var index = 0;
            $(agentArray).each(function () {
                if (this == treeNode.RelationCode) {
                    return false;
                }
                index++;
            });
            if (index < agentArray.length - 1) {
           
                relationCode = agentArray[index + 1];
                accountTarget = treeNode.Account;
                parentNode = treeNode; ;
                $('#selectMember', navTab.getCurrentPanel()).click();

            }
            else {
                alert('已超出定义的层数');
            }

            return false;
        });
    };



    function removeHoverDom(treeId, treeNode) {
        $("#addBtn_" + treeNode.tId).unbind().remove();
    };

  
</script>

 
</div>
	</div>
	










<div class="unitBox" style="margin-left:360px;">
	<div class="pageHeader" style="border:1px #B8D0D6 solid">
	<div class="searchBar">
		<table class="searchContent">
			<tr>
				<td><h3>详细设置</h3>
				</td>

			</tr>
		</table>
	</div>
</div>	


<div class="pageContent" style="border:1px #B8D0D6 solid;"  layoutH="70">

<div class="pageFormContent" id="defaultpage" layoutH="128">

        <div class="noselect" style="margin:20px;line-height: 22px;">
请选择代理人员
</div>
</div>
<div class="pageFormContent" id="agentpage" style="display:none" layoutH="128">

        <dl class="nowrap">
            <dt>帐号：</dt>
        <dd>
            <input id="account_id" name="member.Account" type="hidden"/>
            <input id="account_name" name="member.Account" class="readonly required" type="text" style="margin-top:2px"/>
           <%-- <a class="button" href="/SystemManage/Basics/Shop/Agent/SelectAgentDialog.aspx" lookupGroup="member"><span>选择</span></a>--%>
        </dd>
        </dl>

        
        <dl class="nowrap" id="agentBarcode">
            <dt>二维码：</dt>
            <dd class="imgcontainer">
                         <spc:SinglePictureControl ID="fc_open" runat="server" ImageWidth="200" ImageHeight="200"
                    FileCountLimit="1" FileFilters="*.jpg;*.jpeg;*.bmp;*.png;*.gif" FileDesc="图片" HiddenBtn="true"/></dd>
        </dl>

        
     
</div>
<div class="formBar">
	  <%--  <ul>
			<li>
	        <div class="buttonActive"><div class="buttonContent"><button onclick="updateRelation();" type="button">提交</button></div></div>
            </li>
		</ul>--%>
</div>
</div>
<script type="text/javascript">

    function updateRelation() {
        var data = { 
            Id: currentNode.Id,
            AccountSource:$("#account_id",navTab.getCurrentPanel()).val(),
            Status: $("#status", navTab.getCurrentPanel()).val(),
            BarcodeImg:$("#fc_open_value_json",navTab.getCurrentPanel()).val()
        };

        $.get("/SystemManage/Basics/Shop/Agent/AgentInterface.aspx", { opt: "update", data: $.toJSON(data), temp: new Date().valueOf()
        }, function () {
            alertMsg.correct("操作成功");

            currentNode.Account = $("#account_id", navTab.getCurrentPanel()).val();
            currentNode.Name = $("#account_name", navTab.getCurrentPanel()).val();
            currentNode.Status = $("#status", navTab.getCurrentPanel()).val();
            currentNode.BarcodeImg = $("#fc_open_value_json", navTab.getCurrentPanel()).val()
        });
    }


</script>
</div>
	
</div>